package lu.med.praxiswahl.wahlumed.client.patienten;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.table.AbstractTableModel;

import lu.med.praxiswahl.wahlumed.client.utilities.UniCode;
import lu.med.praxiswahl.wahlumed.client.wclient.Main;
import lu.med.praxiswahl.wahlumed.model.beans.EGravTBL;

@SuppressWarnings("serial")
public class EGravTableModel extends AbstractTableModel
{
    public static final int MODE_TRANSFER_MATRICULE = 0;
    public static final int MODE_TRANSFER_INTERVALL = 2;
    public static final int MODE_TRANSFER_VOID = 1;
    private int curRow = -1;
    private ArrayList<EGravDataRow> data = new ArrayList<EGravDataRow>();
    private Main dataManager;
    private String matricule;
    private int mode = MODE_TRANSFER_MATRICULE;
    private String fromData, toData;

    public EGravTableModel(Main dataManager)
    {
	this.dataManager = dataManager;
    }

    public void addRow(String matricule)
    {
	int d, m, y;
	String da = "";
	Calendar c = new GregorianCalendar();
	d = c.get(Calendar.DAY_OF_MONTH);
	m = c.get(Calendar.MONTH) + 1;
	y = c.get(Calendar.YEAR);
	if (d < 10)
	{
	    da += "0" + d + ".";
	}
	else
	{
	    da += d + ".";
	}
	if (m < 10)
	{
	    da += "0" + m + ".";
	}
	else
	{
	    da += m + ".";
	}
	da += y;

	dataManager.getEGravContext().createEgravItem(matricule, da);
	this.transferFromDatabase();
    }

    public void dropCurrentLine()
    {
	if (curRow == -1)
	{
	    return;
	}

	dataManager.getEGravContext()
		.remove_EgravItem(data.get(curRow).getId());

	switch (mode)
	{
	case MODE_TRANSFER_MATRICULE:
	    this.transferFromDatabase();
	    return;
	case MODE_TRANSFER_INTERVALL:
	    this.transferDataByDate(fromData, toData);
	    return;
	case MODE_TRANSFER_VOID:
	    this.transferEmptyDataFromDatabase();
	    return;
	}
    }

    @Override
    public int getColumnCount()
    {
	return 11;
    }

    @Override
    public String getColumnName(int col)
    {
	switch (col)
	{
	case 0:
	    return "Datum";
	case 1:
	    return "n.US SSW rechn.";
	case 2:
	    return "SSL";
	case 3:
	    return "FS";
	case 4:
	    return "BPD (KU)";
	case 5:
	    return "ATD (AU)";
	case 6:
	    return "Herzaktion";
	case 7:
	    return "Kindsbewegung";
	case 8:
	    return "FW";
	case 9:
	    return "Placenta";
	case 10:
	    return "unauff" + UniCode.getae() + "llig - Ja/Nein";
	case 11:
	    return "Bemerkung";
	}
	return "";
    }

    public EGravDataRow getRow(int row)
    {
	return data.get(row);
    }

    @Override
    public int getRowCount()
    {
	return this.data.size();
    }

    @Override
    public Object getValueAt(int row, int column)
    {
	switch (column)
	{
	case 0:
	    return data.get(row).getDatum();
	case 1:
	    return data.get(row).getNUSBer();
	case 2:
	    return data.get(row).getSSL();
	case 3:
	    return data.get(row).getFS();
	case 4:
	    return data.get(row).getBPD();
	case 5:
	    return data.get(row).getATD();
	case 6:
	    return data.get(row).getHerzaktion();
	case 7:
	    return data.get(row).getKindsbewegung();
	case 8:
	    return data.get(row).getFW();
	case 9:
	    return data.get(row).getPlacenta();
	case 10:
	    return data.get(row).getUnauffaellig();
	case 11:
	    return data.get(row).getBemerkung();
	}
	return "";
    }

    @Override
    public boolean isCellEditable(int row, int col)
    {
	return true;
    }

    public void refresh()
    {
	switch (mode)
	{
	case MODE_TRANSFER_MATRICULE:
	    this.transferFromDatabase();
	    return;
	case MODE_TRANSFER_INTERVALL:
	    this.transferDataByDate(fromData, toData);
	    return;
	case MODE_TRANSFER_VOID:
	    this.transferEmptyDataFromDatabase();
	    return;
	}
    }

    public void setMatricule(String matricule)
    {
	this.matricule = matricule;
	this.transferFromDatabase();
    }

    public void setSelected(int row)
    {
	this.curRow = row;
    }

    @Override
    public void setValueAt(Object v, int row, int column)
    {
	switch (column)
	{
	case 0:
	    data.get(row).setDatum((String) v);
	    break;
	case 1:
	    data.get(row).setNUSBer((String) v);
	    break;
	case 2:
	    data.get(row).setSSL((String) v);
	    break;
	case 3:
	    data.get(row).setFS((String) v);
	    break;
	case 4:
	    data.get(row).setBPD((String) v);
	    break;
	case 5:
	    data.get(row).setATD((String) v);
	    break;
	case 6:
	    data.get(row).setHerzaktion((String) v);
	    break;
	case 7:
	    data.get(row).setKindsbewegung((String) v);
	    break;
	case 8:
	    data.get(row).setFW((String) v);
	    break;
	case 9:
	    data.get(row).setPlacenta((String) v);
	    break;
	case 10:
	    data.get(row).setUnauffaellig((String) v);
	    break;
	case 11:
	    data.get(row).setBemerkung((String) v);
	    break;
	}
	data.get(row).setChanged(true);
	this.transferToDatabase();
    }

    public void transferDataByDate(String fromDate, String toDate)
    {
	data.clear();

	this.fromData = fromDate;
	this.toData = toDate;

	mode = EGravTableModel.MODE_TRANSFER_INTERVALL;

	EGravTBL[] tbl = dataManager.getEGravContext().retrieveItemsBYINTERVAL(
		fromDate, toDate);

	for (int i = 0; i < tbl.length; i++)
	{
	    // Überträgt die Daten in das lokale Modell
	    EGravDataRow dr = new EGravDataRow();
	    dr.setId(tbl[i].getId());
	    dr.setATD(tbl[i].getAtd());
	    dr.setBPD(tbl[i].getBpd());
	    // dr.setBemerkung(tbl[i)
	    dr.setDatum(tbl[i].getDatum());
	    dr.setFS(tbl[i].getFs());
	    dr.setFW(tbl[i].getFw());
	    dr.setHerzaktion(tbl[i].getHerzaktion());
	    dr.setKindsbewegung(tbl[i].getKindsBewegung());
	    dr.setNUSBer(tbl[i].getNUS_SSW());
	    dr.setPlacenta(tbl[i].getPlacenta());
	    dr.setSSL(tbl[i].getSsl());
	    dr.setUnauffaellig(tbl[i].getUnauffaellig());
	    dr.setMatricule(matricule);
	    dataManager.getCustomerContext().getCustomer(matricule);
	    dr.setChanged(false);
	    data.add(dr);
	}
	this.fireTableDataChanged();
    }

    public void transferEmptyDataFromDatabase()
    {
    }

    public void transferFromDatabase()
    {
	if (matricule == null)
	{
	    return;
	}
	if (matricule.equals(""))
	{
	    return;
	}
	data.clear();

	mode = EGravTableModel.MODE_TRANSFER_MATRICULE;

	// Liest die Daten von der Datenbank
	EGravTBL[] tbl = dataManager.getEGravContext().getItemsByCustomer(
		matricule);

	for (int i = 0; i < tbl.length; i++)
	{
	    // Überträgt die Daten in das lokale Modell
	    EGravDataRow dr = new EGravDataRow();
	    dr.setId(tbl[i].getId());
	    dr.setATD(tbl[i].getAtd());
	    dr.setBPD(tbl[i].getBpd());
	    // dr.setBemerkung(tbl[i)
	    dr.setDatum(tbl[i].getDatum());
	    dr.setFS(tbl[i].getFs());
	    dr.setFW(tbl[i].getFw());
	    dr.setHerzaktion(tbl[i].getHerzaktion());
	    dr.setKindsbewegung(tbl[i].getKindsBewegung());
	    dr.setNUSBer(tbl[i].getNUS_SSW());
	    dr.setPlacenta(tbl[i].getPlacenta());
	    dr.setSSL(tbl[i].getSsl());
	    dr.setUnauffaellig(tbl[i].getUnauffaellig());
	    dr.setMatricule(matricule);
	    dataManager.getCustomerContext().getCustomer(matricule);
	    dr.setChanged(false);
	    data.add(dr);
	}
	this.fireTableDataChanged();
    }

    public void transferToDatabase()
    {
	EGravTBL[] tbl = null;
	switch (mode)
	{
	case MODE_TRANSFER_MATRICULE:
	    tbl = dataManager.getEGravContext().getItemsByCustomer(matricule);
	    break;
	case MODE_TRANSFER_INTERVALL:
	    tbl = dataManager.getEGravContext().retrieveItemsBYINTERVAL(
		    fromData, toData);
	    break;
	}

	// Durchläuft alle erhaltenen Datensätze und macht einen Abgleich
	// mit den ID Nummern, die entsprechenden Daten werden auf den Server
	// geschrieben
	for (int i = 0; i < tbl.length; i++)
	{
	    // Sucht nach Übereinstimmung der ID
	    for (int j = 0; j < data.size(); j++)
	    {
		if (data.get(j).getId().equals(tbl[i].getId()))
		{
		    // Hat entsprechende ID gefunden
		    if (data.get(j).isChanged())
		    {
			// Lokale Daten wurden geändert. Dementsprechend
			// werden die geänderten Daten an den Server
			// übertragen
			dataManager.getEGravContext()
				.getA_EGravInstance(tbl[i]);
			dataManager.getEGravContext().set_Atd(
				data.get(j).getATD());
			dataManager.getEGravContext().set_Bpd(
				data.get(j).getBPD());
			dataManager.getEGravContext().set_Datum(
				data.get(j).getDatum());
			dataManager.getEGravContext().set_Fs(
				data.get(j).getFS());
			dataManager.getEGravContext().set_Fw(
				data.get(j).getFW());
			dataManager.getEGravContext().set_Heraktion(
				data.get(j).getHerzaktion());
			dataManager.getEGravContext().set_KindsBewegung(
				data.get(j).getKindsbewegung());
			dataManager.getEGravContext().set_NUS_SSW(
				data.get(j).getNUSBer());
			dataManager.getEGravContext().set_Placenta(
				data.get(j).getPlacenta());
			dataManager.getEGravContext().set_Ssl(
				data.get(j).getSSL());
			dataManager.getEGravContext().set_Unauffaellig(
				data.get(j).getUnauffaellig());
			data.get(j).setChanged(false);
			break;
		    }
		    else
		    {
			break;
		    }
		}
	    }
	}
    }

}
